package com.icez.designmode

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.icez.base_util_library.WindowManagerUtil
import com.icez.recycleradapter.RecyclerAdapter
import com.icez.recycleradapter.holder.RecyclerViewHolder
import com.icez.verticalhorizontalrolinglibrary.RollingLayoutManager
import com.icez.videoplay.core.VideoPlayLayout
import com.icez.videoplay.enum.VideoPlayType
import java.util.*

class MainActivity : AppCompatActivity(), RollingLayoutManager.OnCurrentPositionListener {
    private var rv:RecyclerView ?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main)



        rv = findViewById<RecyclerView>(R.id.rv)
        val mRollingLayoutManager = RollingLayoutManager(this,LinearLayoutManager.VERTICAL)
        mRollingLayoutManager.setOnCurrentPositionListener(this)
        rv?.layoutManager = mRollingLayoutManager
        val list = ArrayList<String>()
        list.add("数据：")
//        for(index in 0..30){
//            list.add("数据："+index)
//        }

        rv?.adapter = object:RecyclerAdapter<String>(this,R.layout.rv_item_layout,list){
            override fun onBusinessOperation(holder: RecyclerViewHolder, position: Int) {
                val btn = holder.getView<Button>(R.id.btn)
                val rv_item_vpl = holder.getView<VideoPlayLayout>(R.id.rv_item_vpl)

                btn?.setOnClickListener {
//                    PermissionUtil.instance.toOpenOverlayUI(this@MainActivity)

//                    rv_item_vpl?.unMute()
//                    lifecycleScope.launch {
//                        delay(500)
//                        rv_item_vpl?.unMute()
//                    }

//                    rv_item_vpl?.setPlayVideo()
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                        this@MainActivity.enterPictureInPictureMode()
//                    }
                }


            }

        }





//        btn.setOnClickListener {
//            Log.e("icez","click")
//            vpl.setStopVideo()
//
//        }

    }

    override fun onResume() {
        super.onResume()
//        WindowManagerUtil.instance.removeFloatWindow()
    }

    override fun onPause() {
        super.onPause()
//        WindowManagerUtil.instance.addFloatWindow(R.layout.window_layout, setBusiness = {
//            val window_layout_vpl = it?.findViewById<VideoPlayLayout>(R.id.window_layout_vpl)
//            window_layout_vpl?.initConfig(this@MainActivity,
//                VideoPlayType.FACEBOOK,
//                "3024261827613903",
//                "468492531485338","", {width:String,height:String->
//                    Log.e("icez", "fb success")
//                }, {
//                    Log.e("icez", "fb fail")
//                }, playWidth = 220f, playHeight = 125f)
//        })
    }
    private var mCurrentPosition:Int ?= -1
    override fun currentDetachedPositionListener(view: View?, currentPosition: Int?) {

    }

    override fun currentPositionListener(view: View?, currentPosition: Int?) {
        val rv_item_vpl = view?.findViewById<VideoPlayLayout>(R.id.rv_item_vpl)
        val position = currentPosition?:0
        if(mCurrentPosition!=position){
            mCurrentPosition = position
            Log.e("icez","bbbbbbbbbbbbbbbb"+position)
            if(false){
                Log.e("icez","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                val youtubeUrl = "file:///android_asset/aos-yt.html?url=https://www.youtube.com/watch?v=hxZZHwFgTE0&&burl=https://static-download2.tripbay.com//release/user/2/2022-05/1651981456351.jpeg"
                rv_item_vpl?.initConfig(this@MainActivity,
                    VideoPlayType.YOUTUBEWEB,
                    "",
                    "",youtubeUrl, {width:String,height:String->
                        Log.e("icez", "successL : "+width)
//                        rv_item_vpl?.setPlayVideo()
                    }, {
                        Log.e("icez", "fail"+it)
                    })
            }else{
                val imgStr = "https://static-download2.tripbay.com/prod/html/live/fb.html?url=https://www.facebook.com/jacky5318/videos/1353105601866027/&&burl=https://static-download2.tripbay.com//release/user/86/2022-05/1652336571185.jpeg&zb=1652342876679"
//                val imgStr = "file:///android_asset/fb(1).html?url=https://www.facebook.com/YoMEi.livegame/videos/1437743266685796/&&burl=https://static-download2.tripbay.com//release/user/86/2022-05/1652328350142.jpeg"
                rv_item_vpl?.initConfig(this@MainActivity,
                    VideoPlayType.FACEBOOK,
                    "3024261827613903",
                    "468492531485338",imgStr, {width:String,height:String->
//                    rv_item_vpl.unMute()
//                    lifecycleScope.launch {
//                        delay(500)
//                        rv_item_vpl?.unMute()
//                    }
                        Log.e("icez", "fb success")
                    }, {
                        Log.e("icez", "fb fail")
                    })
            }
//        }else{
//            rv_item_vpl?.setPlayVideo()
        }

    }
}